﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TY_Hoist.WinApp.Code
{
    public class GCDemoControl
    {

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
        private static extern void SetForegroundWindow(IntPtr hwnd);
        const uint BM_CLICK = 0xF5; //鼠标点击的消息

        IntPtr hwndPhoto;
        public GCDemoControl()
        {
            hwndPhoto = FindWindow(null, "RecSuite  USB3.0高速工业相机专用软件"); //查找拍照程序的句柄【任务管理器中的应用程序名称】
        }

        public void GetCameraList()
        {

            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "获取相机数量"); //获取按钮快照的句柄
                if (hwndThree == IntPtr.Zero)
                {
                    MessageBox.Show("null");
                }
                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void OpenCamera()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "打开相机"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void CloseCamera()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "关闭相机"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void OpenDisplay()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "启动采集"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void ClosepDisplay()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "停止采集"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void StartRecord()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "开始 录像"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void StopRecord()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "停止 录像"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void OpenRecord()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "打开 录像"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

        public void RecordZhuanHuan()
        {
            if (hwndPhoto != IntPtr.Zero)
            {
                IntPtr hwndThree = FindWindowEx(hwndPhoto, 0, null, "录像格式转换"); //获取按钮快照的句柄

                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口

                System.Threading.Thread.Sleep(500);   //暂停500毫秒

                SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮
            }
            else
            {
                MessageBox.Show("没有启动 UcDemo");

            }
        }

    }
}
